import "package:flutter/material.dart";
import 'package:login_demo/app_theme.dart';
import 'package:login_demo/register.dart';
import 'package:login_demo/widget/TextInput.dart';
import 'package:login_demo/widget/button.dart';
import 'package:oktoast/oktoast.dart';

import 'forgetPassword.dart';
// desc: Login
class Login extends StatefulWidget {
  const Login({Key key}) : super(key: key);

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {

  TextEditingController accountController;
  TextEditingController passwordController;

  @override
  void initState() {
    accountController = new TextEditingController();
    passwordController = new TextEditingController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 15, right: 15),
        child: Column(
          children: [
            SizedBox(height: 80),
            Text('登录', style: TextStyle(
              fontSize: 16,
              color: AppTheme.textBlack,
              fontWeight: FontWeight.bold
            )),
            SizedBox(height: 30),
            Padding(
              padding: EdgeInsets.only(left: 10, right: 10),
              child: TextInput(
                icon: Icon(Icons.phone_android_rounded),
                hintText: '请输入账号',
                textEditingController: accountController,
              ),
            ),
            SizedBox(height: 15),
            Padding(
              padding: EdgeInsets.only(left: 10, right: 10),
              child: TextInput(
                icon: Icon(Icons.lock),
                hintText: '请输入密码',
                textEditingController: passwordController,
              ),
            ),
            SizedBox(height: 80),
            Button(
              text: '登录',
              onTap: (){
                _login();
              },
            ),
            SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  onTap: (){
                    _gotoRegister();
                  },
                  child: Text('注册', style: TextStyle(fontSize: 14, color: AppTheme.textBlack)),
                ),
                GestureDetector(
                  onTap: (){
                    _forgetPassword();
                  },
                  child: Text('忘记密码？', style: TextStyle(fontSize: 14, color: AppTheme.dark)),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  _login(){
    if(accountController.text == ''){
      showToast('账号不可为空');
    } else if(passwordController.text == ''){
      showToast('密码不可为空');
    } else {
      // 此处验证账号是否对应相应密码
      // ...
      showToast('登录成功!');
    }
  }
  _gotoRegister(){
    Navigator.push<dynamic>(
      context,
      MaterialPageRoute(
          builder: (BuildContext context) => Register()
      )
    );
  }
  _forgetPassword(){
    Navigator.push<dynamic>(
        context,
        MaterialPageRoute(
            builder: (BuildContext context) => forgetPassword()
        )
    );
  }
}